<?php
/*
Plugin Name: Tabbed Popular & Recent Posts Widget
Plugin URI: http://www.skatdesign.com/
Description: A simple widget to display the popular posts and recent posts in tabs.
Version: 1.00
Author: Skat
Author URI: http://www.skatdesign.com/
*/

// The widget class
class tabbed_widget extends WP_Widget {
	
	// Widget Settings
	function tabbed_widget() {
	
		$widget_ops = array( 'classname' => 'tabbed_widget', 'description' => __('A widget to display tabs with popular and recent posts.', 'framework') );
		$control_ops = "";
		$this->WP_Widget( 'tabbed_widget', __('Tabbed Popular &amp; Recent Posts', 'framework'), $widget_ops, $control_ops );
	}
	
	// Widget Output
	function widget($args, $instance) {
		extract($args);
		$title = apply_filters('widget_title', $instance['title']);

		// Before the widget
		echo $before_widget;

		// Display the widget title if one was input
		if ( $title )
		echo $before_title . $title . $after_title;
		
		// Display the Feedburner subscribe form
		?>

<div class="tabbed-widget">
	<div class="the-tabs">
		<ul class="tabbed-tabs">
			<li>
				<?php _e('Popular Posts', 'framework'); ?>
				<span></span>
			</li>
			<li>
				<?php _e('Recent Posts', 'framework'); ?>
				<span></span>
			</li>
		</ul>
		<div class="tabbed-panes">
			<div class="pane">
				<div class="popular-posts">
					<ul>
						<?php 
			$popular_posts = new WP_Query();
			$popular_posts->query('showposts='.$instance['postcount'].'&orderby=comment_count');
			while ($popular_posts->have_posts()) : $popular_posts->the_post(); ?>
						<li>
							<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) : /* if post has post thumbnail */ ?>
							<div class="recent-blog-thumb">
								<figure>
									<?php the_post_thumbnail('recent-blog'); ?>
								</figure>
							</div>
							<!--image-->
							<?php endif; ?>
							<div class="recent-blog-content">
								<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
									<?php the_title(); ?>
									</a></h4>
								<span class="recent-date">
								<?php echo get_the_date( get_option('date_format') ); ?>
								</span>
							</div>
							<!--details--> 
						</li>
						<?php endwhile; ?>
						<?php wp_reset_query(); ?>
					</ul>
				</div>
			</div>
			<div class="pane">
			<div class="popular-posts">
					<ul>
						<?php 
			$popular_posts = new WP_Query();
			$popular_posts->query('showposts='.$instance['postcount'].'');
			while ($popular_posts->have_posts()) : $popular_posts->the_post(); ?>
						<li>
							<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) : /* if post has post thumbnail */ ?>
							<div class="recent-blog-thumb">
								<figure>
									<?php the_post_thumbnail('recent-blog'); ?>
								</figure>
							</div>
							<!--image-->
							<?php endif; ?>
							<div class="recent-blog-content">
								<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
									<?php the_title(); ?>
									</a></h4>
								<span class="recent-date">
								<?php echo get_the_date( get_option('date_format') ); ?>
								</span>
							</div>
							<!--details--> 
						</li>
						<?php endwhile; ?>
						<?php wp_reset_query(); ?>
					</ul>
				</div>
			</div>
			
			</div>
		</div>
	</div>
</div>
<?php 
		// After the widget
		echo $after_widget;
	}
	// Update the widget		
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['postcount'] = strip_tags( $new_instance['postcount'] );

		return $instance;
	}

	// Widget panel settings
	function form( $instance ) {

	// Default widgets settings
		$defaults = array(
		'title' => __('', 'framework'),
		'postcount' => '3'
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

<!-- Widget Title: Text Input -->
<p>
	<label for="<?php echo $this->get_field_id( 'title' ); ?>">
		<?php _e('Title:', 'framework') ?>
	</label>
	<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
</p>

<!-- Post Count: Text Input -->
<p>
	<label for="<?php echo $this->get_field_id( 'postcount' ); ?>">
		<?php _e('Post Count', 'framework') ?>
	</label>
	<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'postcount' ); ?>" name="<?php echo $this->get_field_name( 'postcount' ); ?>" value="<?php echo $instance['postcount']; ?>" />
</p>
<?php
	}
}
// Register and load the widget
function tabbed_widget() {
	register_widget( 'tabbed_widget' );
}
add_action( 'widgets_init', 'tabbed_widget' );
?>
